import React from 'react';
import ReactDOM from 'react-dom';
import { Router, browserHistory } from 'react-router';
import { syncHistoryWithStore, } from 'react-router-redux';
import { Provider } from 'react-redux';

import setAuthorizationToken from './utils/jwtToken';
import jwtDecode from 'jwt-decode';
import { setCurrentUser } from './actions/authActions';

import configureStore from './configureStore';
import routes from './routes';
import './index.css';

const store = configureStore();
const history = syncHistoryWithStore(browserHistory, store);

if ( localStorage.jwtToken ) {
  try {
    let jwtdecode = jwtDecode(localStorage.jwtToken);
    setAuthorizationToken(localStorage.jwtToken);
    store.dispatch(setCurrentUser(jwtdecode));
  }catch(err){
    // console.log(err);
  }
}

ReactDOM.render(
  <Provider store={store}>
    <Router history={history} routes={routes} />
  </Provider>,
  document.getElementById('root')
);
